using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ChuanPlus.FormControl
{
    public partial class FormLogin : DevExpress.XtraEditors.XtraForm
    {
        public bool authenticated = false;
        public bool isAdmin = false;
        const string LOGIN_FAILED = "Login failed, username or password incorrect!";
        public Users user = null;
        public FormLogin()
        {
            InitializeComponent();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            login();
        }
        private void login()
        {
            user = ChuanPlus.DAO.UserDAO.Authenticate(textUsername.Text, textPassword.Text,ref authenticated, ref isAdmin);
            if (authenticated)
            {                
                this.Close();
            }
            else
                lblError.Text = LOGIN_FAILED;
        }
        private void textPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
            	login();
            }
        }

        private void textUsername_Validating(object sender, CancelEventArgs e)
        {
            if (textUsername.Text.Trim() == "")
            {
                e.Cancel = true;
                textUsername.ErrorText = "Please enter username";
            }
        }

        private void textPassword_Validating(object sender, CancelEventArgs e)
        {
            if (textPassword.Text.Trim() == "")
            {
                e.Cancel = true;
                textPassword.ErrorText = "Please enter Password";
            }
        }

        private void FormLogin_Load(object sender, EventArgs e)
        {

        }

    }
}